Medium.js keeps HTML code within contenteditable
semantic, simple, and clean. Medium.js also supports placeholders, automatic HR (or BR, or P) creation, events, hotkeys, simple & complex element injection, and more!
Medium.js is html5 compliant and has been tested in Firefox, Chrome, Safari, Opera, & IE8+. Browser support is constantly improving!
This script is inspired by Medium's beautiful content editor, something we have aspired to emulate since its inception.
This code is brought to you by:
(cmd/ctrl+[b,u,i])
input
or textarea
Simply include the dependencies, Medium.js library, CSS, and you are ready to rock.
his is Medium.j
The code within Medium.js editors are very semantic and organized, giving you peace of mind with using contenteditable
.
All options (excluding modifiers
, tags
, and cssClasses
) may be overridden with data-medium-*
attributes on the element.
element
: The element in which you want to apply Medium.js to.modifier
: The modifier you want for making things bold/italic etc. May be set to 'auto'
, 'cmd'
, or 'ctrl'
. placeholder
: An optional string that displays when the editor is empty autofocus
: Set the focus to the specified element automatically autoHR
: An HR tag will automatically be inserted if there are two empty paragraphs in succession mode
: Dictate the mode in which you want to allow editing. Currently there are three:
Medium.inlineMode
or "inline"
allows for no rich text editing, and no newlines. Similar to an input
element.Medium.partialMode
or "partial"
is like a textarea
, but allows support for paragraphs.Medium.richMode
or "rich"
allows styling of the text, paragraphs, and hr tags.Medium.inlineRichMode
or "rich"
allows styling of the text, but no new lines.maxLength
: The max size you want to allow for the editor modifiers[{Number}|{String}] = function(event, element) {};
:
Key modifiers from event.keyCode
that will be used to apply the respective styling changes or hook into events.
tags
: Dictates allowed tags. Can be set to null
for extreme freedom.tags.break
is the element that will be created for line breaks.tags.horizontalRule
is the element that will be created for a horizontal rule.tags.paragraph
is the element that will be created for paragraphs.tags.outerLevel
are the only elements allowed to replace a top-level tag.tags.innerLevel
are the only tags allowed within the parent tags. No additional tags are allowed unless included here.attributes
: Dictates attributes. Can be set to null
for extreme freedom.attributes.remove
are attributes to be removed.cssClasses
: Various classes used within Medium.js, customizable to your liking. beforeInvokeElement()
:
Triggered just before an element is invoked.
An element is invoked over a highlighted area.
The this
within the context of this function is Medium.Element
that is about to be invoked.
beforeInsertHtml()
:
Triggered just before html is added.
Inserted at cursor, but only if medium's element has focus.
The this
within the context of this function is Medium.Html
that is about to be inserted.
beforeAddTag(tag, shouldFocus, isEditable, afterElement)
:
Triggered just before a tag is told to be created from a keyboard action.
keyContext[{Number}|{String}] = function(event, element) {};
:
Triggered just before a keydown event.
Can return false or true to override existing functionality, or null to continue.
The element
is the element where the caret currently resides.
Methods provide functionality that can be used with buttons, toolbars, WYSIWYG editors, and the environment it is being used with.
destroy()
: Destroy the instantiated medium.js invokeElement({String}tagName, {Object} [attributes])
: invokes an element.insertHtml({String|HtmlElement} html, {Function} [callback])
: inserts html into the editor at the cursor.makeUndoable()
: makes the current state of the editor undoable/redoable. redo()
: redo an edit. undo()
: undo an edit. value({String} [value])
: set or get value from medium.Medium.js allows you to take total control over contenteditable
elements. This means that you can use contenteditable
without fearing your user is going to enter poor or invalid HTML. It keeps things tight, organized, and semantic.
Yes... Coming soon. The focus of Medium.js has been to make introducing a toolbar, any toolbar, YOUR toolbar, ultra easy. Take a look at wikiLingo for example. More to come!
Yup. This is because of you. But if you find a bug, please let us know or fork Medium.js and contribute so we can continue wide support!
Need commercial support for Medium.js? Currently provided by Visual Interop Development llc., drop us a line, we can't wait to see what we can do together.
MIT Licensing
Copyright (c) 2013 Jacob Kelley
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.